package com.itheima.mapper.impl;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;

import java.io.InputStream;
import java.util.List;

public class UserMapperImpl implements UserMapper {
    //设置全局变量
    private SqlSession session = null;

    //在@Test之前执行
    @Before
    /*public void before() throws Exception {
        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        session = factory.openSession();
    }*/

    //在@Test之后执行
    @After
    /*public void after() {
        //5、事务提交(查询事务用事务，没事务就算了)
        session.commit();
        //6、释放资源
        session.close();
    }*/

    //查询所有用户信息
    public List<User> selectAll() throws Exception {
        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        session = factory.openSession();
        //4、调用具体的statement对象，执行sql
        List<User> list = session.selectList("UserMapper.selectAll");
        //5、事务提交(查询事务用事务，没事务就算了)
        session.commit();
        //6、释放资源
        session.close();
        is.close();
        return list;
    }

    //根据id查询
    public User selectById(Integer uid) throws Exception {
        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        session = factory.openSession();
        //4、调用具体的statement对象，执行sql
        User user = session.selectOne("UserMapper.selectById", uid);
        //5、事务提交(查询事务用事务，没事务就算了)
        session.commit();
        //6、释放资源
        session.close();
        is.close();
        return user;
    }

    //添加用户信息
    public Integer insertUser(User user) throws Exception {
        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        session = factory.openSession();
        //4、调用具体的statement对象，执行sql
        Integer users = session.insert("UserMapper.insertUser",user);
        //5、事务提交(查询事务用事务，没事务就算了)
        session.commit();
        //6、释放资源
        session.close();
        is.close();
        return users;
    }

    //修改用户信息
    public Integer updateUser(User user) throws Exception {
        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        session = factory.openSession();
        //4、调用具体的statement对象，执行sql
        Integer users = session.update("UserMapper.updateUser",user);
        //5、事务提交(查询事务用事务，没事务就算了)
        session.commit();
        //6、释放资源
        session.close();
        is.close();
        return users;
    }

    //删除用户信息
    public Integer deleteUser(Integer uid) throws Exception {
        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        session = factory.openSession();
        //4、调用具体的statement对象，执行sql
        Integer users = session.delete("UserMapper.deleteUser",uid);
        //5、事务提交(查询事务用事务，没事务就算了)
        session.commit();
        //6、释放资源
        session.close();
        is.close();
        return users;
    }
}
